﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using UserCtrl;

namespace GRWL.BZSC.UI
{
    public partial class SelectMaterialSpecFrm : BaseFrm
    {
        private decimal sum = 0;//所选数量之和

        #region
        public int material;//物资代码id

        public string materialCode//物资代码
        {
            set
            {
                this.txtMNo.Text = value;
            }
        }

        public string materialName//物资名称
        {
            set
            {
                this.txtMName.Text = value;
            }
        }

        public string specs//规格
        {
            set
            {
                this.txtSpecs.Text = value;
            }
        }

        public string Num//数量
        {
            set
            {
                this.txtNum.Text = value;
            }
        }

        public int scdid;//生产单id
        public string scd;

        public int gxid;//工序
        public string gx;//id

        public int prdnMaterialId;

        //public XRecvDetail[] _XRecvDetail { get; set; }// TB_User _User { get; set; }

        //public TB_RecvDetail _TB_RecvDetail { get; set; }

        #endregion

        public SelectMaterialSpecFrm()
        {
            InitializeComponent();
        }


        private void SelectMaterialSpecFrm_Load(object sender, EventArgs e)
        {
            //XPrdnWorkstage[] w = BaseDataProxy.GetXPrdnWorkstages(Convert.ToInt32(this.dtpData.RCText(i, "id")));//跟据明细id得到对应工序
            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
            this.BaseDataProxy.Open();

            BindData();

        }


        private void BindData()
        {
            XStock[] xs = BaseDataProxy.GetBatchStocks(1);

            if (xs == null)
                return;

            for (int i = 0; i < xs.Count(); i++)
            {
                int j = dtpData.Rows.Add();
                this.dtpData.Rows[j].Cells["id"].Value = xs[i].Stock.id;
                this.dtpData.Rows[j].Cells["batchNo"].Value = xs[i].Stock.batchNo;
                this.dtpData.Rows[j].Cells["depot"].Value = xs[i].Stock.depot;
                this.dtpData.Rows[j].Cells["depotName"].Value = xs[i].DepotName;
                this.dtpData.Rows[j].Cells["num"].Value = xs[i].Stock.num;
                this.dtpData.Rows[j].Cells["num2"].Value = 0;
                this.dtpData.Rows[j].Cells["unit"].Value = xs[i].Unit; 
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            Common.OperateType opType = Common.OperateType.Add;

            try
            {
                #region
                DataTable dt = new DataTable();
                dt.Columns.Add("material");
                dt.Columns.Add("materialCode");
                dt.Columns.Add("materialName");
                dt.Columns.Add("specs");
                dt.Columns.Add("batchNo");//批号
                dt.Columns.Add("depot");//仓库id
                dt.Columns.Add("depotName");//仓库名称
                dt.Columns.Add("num");
                dt.Columns.Add("unit");
                dt.Columns.Add("scdid");//生产id
                dt.Columns.Add("scd");//生产单号
                dt.Columns.Add("gxid");//工序
                dt.Columns.Add("gx");//工序
                dt.Columns.Add("prdnMaterialId");//生产单明细id

                for (int i = 0; i < this.dtpData.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[i].Cells[0]);
                    if (c.Value != null && (bool)c.Value)
                    {
                        DataRow dr = dt.NewRow();
                        dr["material"] = material;
                        dr["materialCode"] = this.txtMNo.Text;
                        dr["materialName"] = this.txtMName.Text;
                        dr["specs"] = this.txtSpecs.Text;
                        dr["batchNo"] = this.dtpData.RCText(i, "batchNo");
                        dr["depot"] = this.dtpData.RCText(i, "depot");
                        dr["depotName"] = this.dtpData.RCText(i, "depotName");
                        dr["num"] = this.dtpData.RCText(i, "num2");
                        dr["unit"] = this.dtpData.RCText(i, "unit");
                        dr["scdid"] = scdid;
                        dr["scd"] = scd;
                        dr["gxid"] = gxid;
                        dr["gx"] = gx;
                        dr["prdnMaterialId"] = prdnMaterialId;
                        dt.Rows.Add(dr);
                    }
                }
                #endregion

                #region
                //int SelectSum = 0;
                //for (int i = 0; i < this.dtpData.Rows.Count; i++)
                //{
                //    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[i].Cells[0]);
                //    if (c.Value != null && (bool)c.Value)
                //    {
                //        SelectSum = SelectSum + 1;
                //    }
                //}

                //if (SelectSum <= 0)
                //    return;

                //_XRecvDetail = new XRecvDetail[SelectSum];

                //for (int i = 0; i < this.dtpData.Rows.Count; i++)
                //{
                //    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[i].Cells[0]);
                //    if (c.Value != null && (bool)c.Value)
                //    {
                //        SelectSum = SelectSum + 1;

                //        _XRecvDetail[i].RecvDetail.id = material;
                //        _XRecvDetail[i].RecvDetail.materialCode = this.txtMNo.Text;
                //        _XRecvDetail[i].MaterialName = this.txtMName.Text;
                //        _XRecvDetail[i].RecvDetail.specs = this.txtSpecs.Text;
                //        _XRecvDetail[i].Unit = dtpData.RCText(i, "unit");
                //        _XRecvDetail[i].RecvDetail.num = Convert.ToInt32(dtpData.RCText(i, "num"));
                //        _XRecvDetail[i].RecvDetail.depot = Convert.ToInt32(dtpData.RCText(i, "depot"));
                //        _XRecvDetail[i].DepotName = dtpData.RCText(i, "num");
                //        _XRecvDetail[i].RecvDetail.batchNo = dtpData.RCText(i, "batchNo");
                //        //_XRecvDetail[i].RecvDetail.workstage = gx;
                //        //_XRecvDetail[i].RecvDetail.requirement=
                //    }
                //}
                #endregion

                CommonEventArg<DataTable> arg = new CommonEventArg<DataTable>(dt, opType);//使用委托回调给领料窗口
                GRWL.BZSC.Common.EventCommon.OnSelectMaterialSpec(sender, arg);
                this.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.Close();
        }

        private void dtpData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                try
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells["select"]);
                    if (c.Value != null && (bool)c.Value)
                    {
                        ((DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells["select"])).Value = false;
                        dtpData.SetDgRowDefaultBackColor(e.RowIndex, Color.Azure);
                        sum = sum - Convert.ToDecimal(dtpData.RCText(e.RowIndex, "num2"));

                        dtpData.Rows[e.RowIndex].Cells["num2"].Value = 0.0;
                    }
                    else
                    {
                        if (sum >= Convert.ToDecimal(this.txtNum.Text))
                        {
                            MessageBox.Show("数量已够，不需要再选！！");
                            return;
                        }

                        ((DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells["select"])).Value = true;
                        dtpData.SetDgRowDefaultBackColor(e.RowIndex, Color.White);

                        if (Convert.ToDecimal(dtpData.RCText(e.RowIndex, "num")) >= Convert.ToDecimal(this.txtNum.Text))//取库存与所需数量的最小值
                        {
                            dtpData.Rows[e.RowIndex].Cells["num2"].Value = Convert.ToDecimal(this.txtNum.Text) - sum;
                        }
                        else
                            dtpData.Rows[e.RowIndex].Cells["num2"].Value = dtpData.RCText(e.RowIndex, "num");

                            sum = sum + Convert.ToDecimal(dtpData.RCText(e.RowIndex, "num2"));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
